SUBDIRS = .

# Define the flags used in each set of tests.
AM_CPPFLAGS  = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib
AM_CPPFLAGS += $(BOOST_INCLUDES) $(GTEST_INCLUDES) $(LOG4CPLUS_INCLUDES)

AM_CXXFLAGS = $(KEA_CXXFLAGS)

AM_LDADD = $(top_builddir)/src/lib/log/libkea-log.la
AM_LDADD += $(top_builddir)/src/lib/util/unittests/libutil_unittests.la
AM_LDADD += $(top_builddir)/src/lib/exceptions/libkea-exceptions.la
AM_LDADD += $(LOG4CPLUS_LIBS) $(GTEST_LDADD)

AM_LDFLAGS  =
if USE_STATIC_LINK
AM_LDFLAGS += -static
endif

EXTRA_DIST = log_test_messages.mes

if HAVE_GTEST

CLEANFILES = *.gcno *.gcda *.lock

# Helper programs used in shell tests
TEST_HELPERS = logger_example
logger_example_SOURCES = logger_example.cc
logger_example_CPPFLAGS = $(AM_CPPFLAGS)
logger_example_CXXFLAGS = $(AM_CXXFLAGS)
logger_example_LDADD = $(AM_LDADD)
logger_example_LDFLAGS = $(AM_LDFLAGS)

TEST_HELPERS += init_logger_test
init_logger_test_SOURCES = init_logger_test.cc
init_logger_test_CPPFLAGS = $(AM_CPPFLAGS)
init_logger_test_CXXFLAGS = $(AM_CXXFLAGS)
init_logger_test_LDADD = $(AM_LDADD)
init_logger_test_LDFLAGS = $(AM_LDFLAGS)

TEST_HELPERS += buffer_logger_test
buffer_logger_test_SOURCES = buffer_logger_test.cc
buffer_logger_test_CPPFLAGS = $(AM_CPPFLAGS)
buffer_logger_test_CXXFLAGS = $(AM_CXXFLAGS)
buffer_logger_test_LDADD = $(AM_LDADD)
buffer_logger_test_LDFLAGS = $(AM_LDFLAGS)

TEST_HELPERS += logger_lock_test
logger_lock_test_SOURCES  = logger_lock_test.cc
logger_lock_test_SOURCES += log_test_messages.cc log_test_messages.h
logger_lock_test_CPPFLAGS = $(AM_CPPFLAGS)
logger_lock_test_CXXFLAGS = $(AM_CXXFLAGS)
logger_lock_test_LDADD = $(AM_LDADD)
logger_lock_test_LDFLAGS = $(AM_LDFLAGS)

# Don't install helper binaries.
noinst_PROGRAMS = $(TEST_HELPERS)

# Shell tests. These are principally tests where the global logging environment
# is affected, and where the output needs to be compared with stored output
# (where "cut" and "diff" are useful utilities).
SHTESTS  =
SHTESTS += buffer_logger_test.sh
SHTESTS += console_test.sh
SHTESTS += destination_test.sh
SHTESTS += init_logger_test.sh
SHTESTS += local_file_test.sh
SHTESTS += logger_lock_test.sh
SHTESTS += severity_test.sh

# As with every file generated by ./configure, clean them up when running
# "make distclean", but not on "make clean".
DISTCLEANFILES = tempdir.h
DISTCLEANFILES += $(SHTESTS)

TESTS_ENVIRONMENT = $(LIBTOOL) --mode=execute $(VALGRIND_COMMAND)

# Set of unit tests for the general logging classes
PROGRAM_TESTS = run_unittests
run_unittests_SOURCES  = run_unittests.cc
run_unittests_SOURCES += log_formatter_unittest.cc
run_unittests_SOURCES += logger_level_impl_unittest.cc
run_unittests_SOURCES += logger_level_unittest.cc
run_unittests_SOURCES += logger_manager_unittest.cc
run_unittests_SOURCES += logger_name_unittest.cc
run_unittests_SOURCES += logger_support_unittest.cc
run_unittests_SOURCES += logger_unittest.cc
run_unittests_SOURCES += logger_specification_unittest.cc
run_unittests_SOURCES += message_dictionary_unittest.cc
run_unittests_SOURCES += message_reader_unittest.cc
run_unittests_SOURCES += output_option_unittest.cc
run_unittests_SOURCES += buffer_appender_unittest.cc
run_unittests_SOURCES += log_test_messages.cc log_test_messages.h
run_unittests_CPPFLAGS = $(AM_CPPFLAGS)
run_unittests_CXXFLAGS = $(AM_CXXFLAGS)
run_unittests_LDADD = $(AM_LDADD)
run_unittests_LDFLAGS = $(AM_LDFLAGS)

# logging initialization tests.  These are put in a separate program to
# ensure that the initialization status at the start of each test is known,
# and to prevent circumstances where the execution of one test affects the
# starting conditions of the next.
PROGRAM_TESTS += initializer_unittests_1
initializer_unittests_1_SOURCES  = run_initializer_unittests.cc
initializer_unittests_1_SOURCES += message_initializer_1_unittest.cc
initializer_unittests_1_SOURCES += message_initializer_1a_unittest.cc
initializer_unittests_1_CPPFLAGS = $(AM_CPPFLAGS)
initializer_unittests_1_CXXFLAGS = $(AM_CXXFLAGS)
initializer_unittests_1_LDADD = $(AM_LDADD)
initializer_unittests_1_LDFLAGS = $(AM_LDFLAGS)

# Run C++ tests on "make check".
TESTS = $(PROGRAM_TESTS)

# Run shell tests on "make check".
check_SCRIPTS = $(SHTESTS)
TESTS += $(SHTESTS)

# Don't install test binaries.
noinst_PROGRAMS += $(PROGRAM_TESTS)

endif

# Don't install shell tests.
noinst_SCRIPTS = $(SHTESTS)

# If we want to get rid of all generated messages files, we need to use
# make maintainer-clean. The proper way to introduce custom commands for
# that operation is to define maintainer-clean-local target. However,
# make maintainer-clean also removes Makefile, so running configure script
# is required.  To make it easy to rebuild messages without going through
# reconfigure, a new target messages-clean has been added.
maintainer-clean-local:
	rm -f log_test_messages.h log_test_messages.cc

# To regenerate messages files, one can do:
#
# make messages-clean
# make messages
#
# This is needed only when a .mes file is modified.
messages-clean: maintainer-clean-local

if GENERATE_MESSAGES

# Define rule to build logging source files from message file
messages: log_test_messages.h log_test_messages.cc
	@echo Message files regenerated

log_test_messages.h log_test_messages.cc: log_test_messages.mes
	$(top_builddir)/src/lib/log/compiler/kea-msg-compiler $(top_srcdir)/src/lib/log/tests/log_test_messages.mes

else

messages log_test_messages.h log_test_messages.cc:
	@echo Messages generation disabled. Configure with --enable-generate-messages to enable it.

endif
